package cn.firecode.data_collect.controller;


import cn.firecode.data_collect.domain.entity.CollectBoardT;
import cn.firecode.data_collect.domain.entity.CollectDeviceT;
import cn.firecode.data_collect.service.CollectBoardTService;
import cn.firecode.data_collect.utils.DataResponse;
import cn.firecode.data_collect.utils.FastDFSClientUtil;
import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.time.LocalDate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangfan
 * @since 2019-12-11
 */
@RestController
@RequestMapping("/board")
@Slf4j
public class CollectBoardTController {

    @Autowired
    private CollectBoardTService boardTService;

    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;


    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public DataResponse add(@RequestParam("designFile") MultipartFile designFile, @RequestParam("describe") String describe,
                            String name, String designFileMD5){
        log.info("新增工装板信息");
        try {
            String localDesignFileMD5 = SecureUtil.md5(designFile.getInputStream());
            log.info("Receive Design File the File MD5 is:"+localDesignFileMD5);
            if(!localDesignFileMD5.toLowerCase().equals(designFileMD5.toLowerCase())){
                log.error("Desing File MD5校验未通过:"+localDesignFileMD5);
                return DataResponse.failed(7,"Desing File MD5校验未通过，请重新上传");
            }
            log.info("开始上传DesignFile........");
            String desingFileFastDFSPath = fastDFSClientUtil.uploadFile(designFile);
            log.info("上传DesignFile成功:"+desingFileFastDFSPath);
            CollectBoardT boardT = new CollectBoardT();
            boardT.setBoardDesignFile(desingFileFastDFSPath);
            boardT.setBoardName(name);
            boardT.setCreateTime(LocalDate.now());
            boardT.setDescribeInfo(describe);
            boardT.setIsDelect(0);
            boolean saveResult = boardTService.save(boardT);
            if(saveResult){
                log.info("新增工装板信息成功:"+boardT.toString() );
                return DataResponse.succeed("新增成功");
            }else{
                log.error("数据库存储出错");
                return DataResponse.failed(1,"新增失败");
            }
        } catch (IOException e) {
            log.error("FastDFS 文件服务器上传失败");
            return DataResponse.failed(7,"FastDFS 文件服务器上传失败");
        }


    }


    @RequestMapping(value = "/delete/{boardId}", method = RequestMethod.GET)
    public DataResponse delete(@PathVariable("boardId") Integer boardId) {

        CollectBoardT byId = boardTService.getById(boardId);
        log.info("删除工装板信息："+byId.toString());
        byId.setIsDelect(1);
        boolean result = boardTService.saveOrUpdate(byId);
        if(result){
            return DataResponse.succeed("删除成功");
        }else{
            return DataResponse.failed(1, "删除成功");
        }
    }

}
